#~ Importamos libreria de la ventana 
from Tkinter import *

#~ Importamos libreria del serial.
import serial
import time

#~ Configuramos el puerto serial.
serial = serial.Serial(port = 0, 
baudrate = 9600,
parity = serial.PARITY_ODD,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS)


#~ Creamos la ventana y sus propiedades.
ventana = Tk()
ventana.title("Trimble4000")
ventana.geometry("615x250")
ventana.resizable(width = FALSE, height = FALSE)
background_image = PhotoImage(file = "fondo.gif")
background_label = Label(ventana, image = background_image)
background_label.place(x=0,y=0,relwidth=1,relheight=1)


#Creamos el label
scrdump = '\x02\x00\x82\x00\x82\x03'
texto1 = Label(ventana,width = 50, height = 1, bg="green", borderwidth=0, font=("Arial", 10), anchor = E)
texto1.place(x=82, y=50)
texto2 = Label(ventana,width = 50, height = 1, bg="green", borderwidth=0, font=("Helvetica", 10), anchor = E)
texto2.place(x=82, y=68)
texto3 = Label(ventana,width = 50, height = 1, bg="green", borderwidth=0, font=("Helvetica", 10), anchor = E)
texto3.place(x=82, y=86)
texto4 = Label(ventana,width = 50, height = 1, bg="green", borderwidth=0, font=("Helvetica", 10), anchor = E)
texto4.place(x=82, y=104)


#Funcion para actualizar pantalla.
def pantallai():
    serial.write(scrdump)
#     serial.read(4)
#     texto1.config(text = '')
#     texto2.config(text = '')
#     texto3.config(text = '')
#     texto4.config(text = '')
#     texto1.config(text=serial.read(40))
#     texto2.config(text=serial.read(40))
#     texto3.config(text=serial.read(40))
#     texto4.config(text=serial.read(40))
#     serial.read(3)
    print serial.read(4)
    print serial.read(40)
    print serial.read(40)
    print serial.read(40)
    print serial.read(40)
    print serial.read(3)

def pantalla():
    time.sleep(0.1)
    serial.write(scrdump)
#     serial.read(4)
#     texto1.config(text = '')
#     texto2.config(text = '')
#     texto3.config(text = '')
#     texto4.config(text = '')
#     texto1.config(text=serial.read(40))
#     texto2.config(text=serial.read(40))
#     texto3.config(text=serial.read(40))
#     texto4.config(text=serial.read(40))
#     serial.read(3)
    print serial.read(5)
    print serial.read(40)
    print serial.read(40)
    print serial.read(40)
    print serial.read(40)
    print serial.read(3)
    
pantallai()

#Valores hexa de los botones
boti = '\x02\x00\x81\x01\x1D\x9F\x03'
botd = '\x02\x00\x81\x01\x1C\x9E\x03'
num1 = '\x02\x00\x81\x01\x31\xB3\x03'
num2 = '\x02\x00\x81\x01\x32\xB4\x03'
num3 = '\x02\x00\x81\x01\x33\xB5\x03'
num4 = '\x02\x00\x81\x01\x34\xB6\x03'
num5 = '\x02\x00\x81\x01\x35\xB7\x03'
num6 = '\x02\x00\x81\x01\x36\xB8\x03'
num7 = '\x02\x00\x81\x01\x37\xB9\x03'
num8 = '\x02\x00\x81\x01\x38\xBA\x03'
num9 = '\x02\x00\x81\x01\x39\xBB\x03'
num0 = '\x02\x00\x81\x01\x30\xB2\x03'
enter = '\x02\x00\x81\x01\x0D\8F\x03'
clear = '\x02\x00\x81\x01\x7F\x01\x03'
alpha = '\x02\x00\x81\x01\x50\xD2\x03'
status = '\x02\x00\x81\x01\x4C\xCE\x03'
satinfo = '\x02\x00\x81\x01\x4B\xCD\x03'
sessions = '\x02\x00\x81\x01\x4A\xCC\x03'
control = '\x02\x00\x81\x01\x4D\xCF\x03'
logdata = '\x02\x00\x81\x01\x4F\xD1\x03'
modify = '\x02\x00\x81\x01\x4E\xD0\x03'
op1 = '\x02\x00\x81\x01\x41\xC3\x03'
op2 = '\x02\x00\x81\x01\x42\xC4\x03'
op3 = '\x02\x00\x81\x01\x43\xC5\x03'
op4 = '\x02\x00\x81\x01\x44\xC6\x03'
power = '\x02\x00\x81\x01\x1B\x9D\x03'


#~ Funciones al precionar los botones.
def funi():
    serial.write(boti)
    pantalla()
    return

def fund():
    serial.write(botd)
    pantalla()
    return
    
def fun1():
    serial.write(num1)
    pantalla()
    return
    
def fun2():
    serial.write(num2)
    pantalla()
    return        
    
def fun3():
    serial.write(num3)
    pantalla()
    return
    
def fun4():
    serial.write(num4)
    pantalla()
    return            

def fun5():
    serial.write(num5)
    pantalla()
    return
    
def fun6():
    serial.write(num6)
    pantalla()
    return        
    
def fun7():
    serial.write(num7)
    pantalla()
    return
    
def fun8():
    serial.write(num8)
    pantalla()
    return    
    
def fun9():
    serial.write(num9)
    pantalla()
    return
    
def fun0():
    serial.write(num0)
    pantalla()
    return        
    
def funenter():
    serial.write(enter)
    pantalla()
    return
    
def funalpha():
    serial.write(alpha)
    pantalla()
    return            

def funclear():
    serial.write(clear)
    pantalla()
    return
    
def funstatus():
    serial.write(status)
    pantalla()
    return    
    
def funsatinfo():
    serial.write(satinfo)
    pantalla()
    return            

def funsessions():
    serial.write(sessions)
    pantalla()
    return
    
def funcontrol():
    serial.write(control)
    pantalla()
    return        
    
def funlogdata():
    serial.write(logdata)
    pantalla()
    return
    
def funmodify():
    serial.write(modify)
    pantalla()
    return        
    
def funop1():
    serial.write(op1)
    pantalla()
    return            

def funop2():
    serial.write(op2)
    pantalla()
    return
    
def funop3():
    serial.write(op3)
    pantalla()
    return        
    
def funop4():
    serial.write(op4)
    pantalla()
    return
    
def funpower():
    serial.write(power)
    pantalla()
    return        
    
    
#Botones.
botoni = Button(ventana,text = '<', command = funi)
botoni.place(bordermode=OUTSIDE, height=25, width=25, x=47, y=58)

botoni = Button(ventana,text = '>', command = fund)
botoni.place(bordermode=OUTSIDE, height=25, width=25, x=47, y=90)

botoni = Button(ventana,text = '1', borderwidth=0, command = fun1)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=83, y=137)
uno=PhotoImage(file="1.gif")
botoni.config(image=uno)

botoni = Button(ventana,text = '2', command = fun2)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=136, y=137)

botoni = Button(ventana,text = '3', command = fun3)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=189, y=137)

botoni = Button(ventana,text = '4', command = fun4)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=83, y=169)

botoni = Button(ventana,text = '5', command = fun5)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=136, y=169)

botoni = Button(ventana,text = '6', command = fun6)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=189, y=169)

botoni = Button(ventana,text = '7', command = fun7)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=83, y=201)

botoni = Button(ventana,text = '8', command = fun8)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=136, y=201)

botoni = Button(ventana,text = '9', command = fun9)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=189, y=201)

botoni = Button(ventana,text = '0', command = fun0)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=242, y=201)

botoni = Button(ventana,text = 'ENTER', command = funenter)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=264, y=137)

botoni = Button(ventana,text = 'ALPHA', command = funalpha)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=264, y=169)

botoni = Button(ventana,text = 'CLEAR', command = funclear)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=317, y=137)

botoni = Button(ventana,text = 'STATUS', command = funstatus)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=384, y=137)

botoni = Button(ventana,text = 'SAT INFO', command = funsatinfo)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=437, y=137)

botoni = Button(ventana,text = 'SESSIONS', command = funsessions)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=384, y=169)

botoni = Button(ventana,text = 'CONTROL', command = funcontrol)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=437, y=169)

botoni = Button(ventana,text = 'LOG DATA', command = funlogdata)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=384, y=201)

botoni = Button(ventana,text = 'MODIFY', command = funmodify)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=437, y=201)

botoni = Button(ventana,text = ' ', command = funop1)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=549, y=29)

botoni = Button(ventana,text = ' ', command = funop2)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=549, y=61)

botoni = Button(ventana,text = ' ', command = funop3)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=549, y=93)

botoni = Button(ventana,text = ' ', command = funop4)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=549, y=125)

botoni = Button(ventana,text = 'POWER', command = funpower)
botoni.place(bordermode=OUTSIDE, height=24, width=42, x=550, y=187)

#Creamos un menu
#puerto = Menu(ventana)
#puerto.add_command(label="Puerto", command=serie)
#ventana.config(menu=puerto)

#def serie(self):
#    puerto.post(event.x_root, event.y_root)

ventana.mainloop()
